package com.java.digitalsignagesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.digitalsignagesystem.mapper.OperateMapper;
import com.java.digitalsignagesystem.entity.Operate;
import com.java.digitalsignagesystem.service.OperateService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * (Operate)表服务实现类
 *
 * @author makejava
 * @since 2023-08-24 10:56:23
 */
@Service("operateService")
public class OperateServiceImpl extends ServiceImpl<OperateMapper,Operate> implements OperateService {
    @Resource
    private OperateMapper operateMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Operate queryById(Integer id) {
        return this.operateMapper.queryById(id);
    }



    /**
     * 新增数据
     *
     * @param operate 实例对象
     * @return 实例对象
     */
    @Override
    public Operate insert(Operate operate) {
        this.operateMapper.insert(operate);
        return operate;
    }

    /**
     * 修改数据
     *
     * @param operate 实例对象
     * @return 实例对象
     */
    @Override
    public Operate update(Operate operate) {
        this.operateMapper.update(operate);
        return this.queryById(operate.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.operateMapper.deleteById(id) > 0;
    }

    @Override
    public List<Operate> selectConcernByUserId(String userId, String theme, LocalDateTime startTime, LocalDateTime currentDateTime) {
        /*LambdaQueryWrapper<Operate> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Operate::getUserId,id);
        queryWrapper.eq(Operate::getConcern,1);
        queryWrapper.eq(Operate::getTheme,theme);
        List<Operate> operates = operateMapper.selectList(queryWrapper);*/
        List<Operate> operates = operateMapper.selectConcernByUserId(userId, theme, startTime, currentDateTime);
        return operates;
    }

    @Override
    public List<Operate> selectByDataId(String indicatorId) {
        LambdaQueryWrapper<Operate> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Operate::getIndicatorId,indicatorId);
        List<Operate> operates = operateMapper.selectList(queryWrapper);
        return operates;
    }

    @Override
    public String getData(String theme) throws IOException {
        String path = "http://10.1.213.229:8310/service/api/zp/sjbdsc?theme="+theme;
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(path)
                .get()
                .addHeader("content-type", "application/json")
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Operate> selectCollectByUserId(String userId, String theme, LocalDateTime startTime, LocalDateTime currentDateTime) {
        List<Operate> operates = operateMapper.selectCollectByUserId(userId, theme, startTime, currentDateTime);
        return operates;
    }

    @Override
    public List<Operate> selectByUserId(String userId) {
        LambdaQueryWrapper<Operate> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Operate::getUserId,userId);
        queryWrapper.eq(Operate::getConcern,1);
        List<Operate> operates = operateMapper.selectList(queryWrapper);
        return operates;
    }
}
